package com.ruoyi.zlwx.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.zlwx.domain.Zlwx;
import com.ruoyi.zlwx.domain.ZlwxFj;
import com.ruoyi.zlwx.service.IZlwxService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 专利文献Controller
 *
 * @author huacai
 * @date 2024-10-16
 */
@RestController
@RequestMapping("/zlwx/zlwx")
public class ZlwxController extends BaseController {
    @Resource
    private IZlwxService zlwxService;

    /**
     * 查询专利文献列表
     */
    @PreAuthorize("@ss.hasPermi('zlwx:zlwx:list')")
    @GetMapping("/list")
    public TableDataInfo list(Zlwx zlwx) {
        startPage();
        List<Zlwx> list = zlwxService.selectZlwxList(zlwx);
        return getDataTable(list);
    }

    /**
     * 导出专利文献列表
     */
    @PreAuthorize("@ss.hasPermi('zlwx:zlwx:export')")
    @Log(title = "专利文献", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Zlwx zlwx) {
        List<Zlwx> list = zlwxService.selectZlwxList(zlwx);
        ExcelUtil<Zlwx> util = new ExcelUtil<Zlwx>(Zlwx.class);
        util.exportExcel(response, list, "专利文献数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Zlwx> util = new ExcelUtil<Zlwx>(Zlwx.class);
        util.importTemplateExcel(response, "专利文献数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "专利文献", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('zlwx:zlwx:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Zlwx> util = new ExcelUtil<Zlwx>(Zlwx.class);
        InputStream inputStream = file.getInputStream();
        List<Zlwx> list = util.importExcel(inputStream);
        inputStream.close();
        int count = zlwxService.batchInsertZlwx(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取专利文献详细信息
     */
    @PreAuthorize("@ss.hasPermi('zlwx:zlwx:query')")
    @GetMapping(value = "/{zlwxId}")
    public AjaxResult getInfo(@PathVariable("zlwxId") String zlwxId) {
        return success(zlwxService.selectZlwxByZlwxId(zlwxId));
    }

    /**
     * 新增专利文献
     */
    @PreAuthorize("@ss.hasPermi('zlwx:zlwx:add')")
    @Log(title = "专利文献", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Zlwx zlwx) {
        zlwx.setUserId(Math.toIntExact(getUserId())); //插入用户ID
        zlwx.setDeptId(Math.toIntExact(getDeptId())); //插入部门ID
        zlwx.setCreateBy(getUsername()); //插入创建人用户名称
        return toAjax(zlwxService.insertZlwx(zlwx));
    }

    /**
     * 修改专利文献
     */
    @PreAuthorize("@ss.hasPermi('zlwx:zlwx:edit')")
    @Log(title = "专利文献", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Zlwx zlwx) {
        return toAjax(zlwxService.updateZlwx(zlwx));
    }

    /**
     * 删除专利文献
     */
    @PreAuthorize("@ss.hasPermi('zlwx:zlwx:remove')")
    @Log(title = "专利文献", businessType = BusinessType.DELETE)
    @DeleteMapping("/{zlwxIds}")
    public AjaxResult remove(@PathVariable String[] zlwxIds) {
        return toAjax(zlwxService.deleteZlwxByZlwxIds(zlwxIds));
    }

    /**
     * 获取专利文献主键ID (UUID)
     */
    @GetMapping("/getZlWxId")
    public AjaxResult getZlWxId() {
        String zlwxId = IdUtils.fastSimpleUUID();
        return success(zlwxId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertZlwxFjByZlwxId(@RequestBody ZlwxFj zlwxFj) {
        return toAjax(zlwxService.insertZlwxFjByZlwxId(zlwxFj));
    }

    /**
     * 根据专利文献ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectZlwxFjList(String sId) {
        ZlwxFj zlwxFj = new ZlwxFj();
        zlwxFj.setZlwxId(sId);
        return AjaxResult.success(zlwxService.selectZlwxFjList(zlwxFj));
    }

    /**
     * 删除专利文献附件
     */
    @Log(title = "专利文献附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/zlwxFj/{fjIds}")
    public AjaxResult deleteZlwxFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(zlwxService.deleteZlwxFjByFjIds(fjIds));
    }

}
